#include "standard.h"
#include "skill.h"
#include "client.h"
#include "carditem.h"
#include "engine.h"
#include "thunder.h"

class XijingPindian: public TriggerSkill{
public:
    XijingPindian():TriggerSkill("#xijing-pindian"){
        events << Pindian;
    }

    virtual int getPriority() const{
        return -1;
    }

    virtual bool trigger(TriggerEvent, ServerPlayer *player, QVariant &data) const{
        PindianStar pindian = data.value<PindianStar>();
        if(pindian->reason != objectName())
            return false;
        if(pindian->isSuccess())
            pindian->from->setMark("xij", 2);
        else
            pindian->from->setMark("xij", 3);
        return false;
    }
};

class Xijing: public TriggerSkill{
public:
    Xijing():TriggerSkill("xijing"){
        events << SlashEffect << SlashProceed;
    }

    virtual bool trigger(TriggerEvent event, ServerPlayer *player, QVariant &data) const{
        Room *room = player->getRoom();
        SlashEffectStruct effect = data.value<SlashEffectStruct>();
        if(event == SlashProceed){
            if(player->getMark("xij") == 2){
                room->setPlayerMark(player, "xij", 0);
                room->slashResult(effect, NULL);
            }
            else if(player->getMark("xij") == 3){
                room->setPlayerMark(player, "xij", 0);
                const Card *jink = room->askForCard(effect.to, ".", "slash-jink-xijing:" + player->objectName(), data);
                room->slashResult(effect, jink);
                return true;
            }
        }
        else{
            if(effect.slash->getEffectiveId() < 0)
                return false;
            if(effect.slash && !effect.to->isKongcheng() && player->askForSkillInvoke(objectName(), data))
                player->pindian(effect.to, objectName(), effect.slash);
        }
        return false;
    }
};

GejiCard::GejiCard(){
    target_fixed = true;
}

void GejiCard::use(Room *room, ServerPlayer *source, const QList<ServerPlayer *> &) const{\
    if(subcardsLength() == 2){
        room->throwCard(this);
        RecoverStruct rec;
        rec.card = this;
        room->recover(source, rec, true);
    }
    else{
        room->loseHp(source);
        if(source->isAlive())
            room->drawCards(source, 2);
    }
}

class Geji: public ViewAsSkill{
public:
    Geji(): ViewAsSkill("geji"){
    }

    virtual bool viewFilter(const QList<CardItem *> &selected, const CardItem *to_select) const{
        return !to_select->isEquipped();
    }

    virtual const Card *viewAs(const QList<CardItem *> &cards) const{
        if(cards.isEmpty())
            return NULL;
        GejiCard *card = new GejiCard;
        card->addSubcards(cards);
        return card;
    }
};

ThunderPackage::ThunderPackage()
    :Package("thunder")
{
    General *kazatokyousuke = new General(this, "kazatokyousuke", "woo");
    kazatokyousuke->addSkill(new Xijing);
    kazatokyousuke->addSkill(new XijingPindian);
    related_skills.insertMulti("xijing", "#xijing-pindian");

    General *kurakimai = new General(this, "kurakimai", "yi", "3/4", General::Female);
    kurakimai->addSkill(new Geji);

    addMetaObject<GejiCard>();
}

ADD_PACKAGE(Thunder)
